package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.bean.AdminInfoBean;
import com.cskaoyan.catmall.bean.InfoData;
import com.cskaoyan.catmall.bean.LoginUserData;
import com.cskaoyan.catmall.config.CustomizedToken;
import com.cskaoyan.catmall.model.pojo.UserPojo;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.service.AdminService;
import com.cskaoyan.catmall.utils.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 登陆接口
 * 创建日期: 2022/01/07 16:00
 *
 * @author ghr
 */

// shiro整合之后，在做具体的开发
// 响应结果都是JSON，Controller组件上我们都用@RestController注解
@RestController
@RequestMapping("admin/auth")
public class AuthController {

    @Autowired
    AdminService adminService;

    /**
     * 如果参数比较少，类型比较简单的话，使用map来接收也可以
     */
    @PostMapping("login")
    public BaseRespVo<LoginUserData> login(@RequestBody Map map) {
        String username = (String)map.get("username");
        String password = (String)map.get("password");

        //Handler方法中使用Shiro的相关内容使用的Subject
        Subject subject = SecurityUtils.getSubject();
        //UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        CustomizedToken admin = new CustomizedToken(username, MD5Util.encrypt(password), "admin");
        subject.login(admin);//执行登录 → 认证器 → realm中的doGetAuthen方法

        boolean authenticated = subject.isAuthenticated();
        System.out.println("authenticate:" + authenticated);

        UserPojo primaryPrincipal = (UserPojo) subject.getPrincipals().getPrimaryPrincipal();
        System.out.println("用户信息" + primaryPrincipal);

        LoginUserData loginUserData = new LoginUserData();

        AdminInfoBean adminInfo = new AdminInfoBean();
        adminInfo.setAvatar(primaryPrincipal.getAvatar());
        adminInfo.setNickName(primaryPrincipal.toString());
        loginUserData.setAdminInfo(adminInfo);
        loginUserData.setToken(subject.getSession().getId().toString());
        System.out.println(subject.getSession().getId().toString());
        return BaseRespVo.ok(loginUserData);
    }

    @RequestMapping("info")
    public BaseRespVo info() {


        UserPojo primaryPrincipal = (UserPojo) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        // 查看用户拥有的所有权限, 返回list集合
        String[] roleIds = primaryPrincipal.getRoleIds();
        List<String> perms = adminService.selectUserPerms(roleIds);
        //System.out.println(perms);

        // 根据RoleId查询角色名称
        List<String> roles = adminService.selectUserRoles(roleIds);

        //System.out.println(perms);

        //开发完shiro之后，再整合
        InfoData infoData = new InfoData();
        infoData.setName(primaryPrincipal.getUsername());

        //根据primaryPrincipal做查询
        infoData.setAvatar(primaryPrincipal.getAvatar());
        //ArrayList<String> roles = new ArrayList<>();
        //roles.add("超级管理员");
        infoData.setRoles(roles);
        //ArrayList<String> perms2 = new ArrayList<>();
        //perms.add("*");
        infoData.setPerms(perms);

        return BaseRespVo.ok(infoData);
    }


    @PostMapping("logout")
    @ResponseBody
    public String logout(){

        Subject subject = SecurityUtils.getSubject();

        if(subject.isAuthenticated()){
            subject.logout();
        }

        return "退出登录成功....";

    }
}

